﻿
using UnityEngine;
using UnityEngine.InputSystem;

namespace Gameplay
{
    public class PlayerController:CharacterController
    {
        
        private SkillManager _skillManager;
        private Vector2 _moveInput;
        private bool _isJumping;
        private bool _isAttacking;

        private void Start()
        {
           _skillManager =_character.SkillManager;
        }
        public void GetMoveInput(InputAction.CallbackContext ctx) 
        {
            _moveInput=ctx.ReadValue<Vector2>();
        }
        public void GetJumpInput(InputAction.CallbackContext ctx) 
        {
            _isJumping = ctx.ReadValueAsButton();
        }
        public void GetAttackInput(InputAction.CallbackContext ctx) 
        {
            _isAttacking = ctx.ReadValueAsButton();
        }

        private void Update()
        {
            if (_character.Attacking||_character.Damage||_character.Dead) 
            {
                return;
            }

       

            if (_skillManager.CurrentUseSkill.Config.TriggerType == TriggerType.Hold) 
            {
                if (Input.GetKeyDown(_skillManager.CurrentUseSkill.Config.Code))
                {
                    _skillManager.UseSkill(1003);
                }
                else if (Input.GetKeyUp(_skillManager.CurrentUseSkill.Config.Code))
                {
                    _skillManager.CanelSkill();
                    _character.IsBlock = false;
                }
            }
            else 
            {
                if (Input.GetKeyDown(_skillManager.CurrentUseSkill.Config.Code))
                {
                    _skillManager.UseSkill(1003);
                }
            }
            

            if (_character.IsBlock) 
            {
                return;
            }
            
            if (_isJumping)
            {
                _character.Jump();
                return;
            }

            if (_moveInput.magnitude > 0.1f)
            {
                Vector3 dir = new Vector3(_moveInput.x, 0, _moveInput.y);
                _character.MoveTo(dir);
            }
            else
            {
                _character.Idle();
            }
        }
    }
}
